package com.offcn.scwproject.controller;

import com.alibaba.fastjson.JSON;
import com.offcn.dycommon.enums.ProjectStatusEnume;
import com.offcn.dycommon.response.AppResponse;
import com.offcn.scwproject.contants.ProjectContants;
import com.offcn.scwproject.pojo.TReturn;
import com.offcn.scwproject.service.ProjectCreateService;
import com.offcn.scwproject.vo.req.ProjectBaseInfoVo;
import com.offcn.scwproject.vo.req.ProjectRedisStorageVo;
import com.offcn.scwproject.vo.req.ProjectReturnVo;
import com.offcn.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;



@Api(tags="项目的创建==初始化")
@Slf4j
@RequestMapping("/project")
@RestController
public class ProjectController {
@Autowired
    private ProjectCreateService projectCreateService;
@Autowired
    private StringRedisTemplate redisTemplate;
@ApiOperation("项目初始化的第一步：")
@RequestMapping("/init")
public AppResponse init(BaseVo vo){
    String accessToken = vo.getAccessToken();
    String memberId = redisTemplate.opsForValue().get(accessToken);
   if (StringUtils.isEmpty(memberId)){
     return AppResponse.fail("请登录");
     }
    int id = Integer.parseInt(memberId);
    //初始化项目
    String s = projectCreateService.initCreatProject(id);
    return AppResponse.ok(s);
}
@ApiOperation("第二步 保存页面基本信息")
@RequestMapping("/savebaseInfo")
public AppResponse saveBaseInfo(ProjectBaseInfoVo vo){
    //.
    String redisProject = redisTemplate.opsForValue().get(ProjectContants.TEMP_PROJECT_PREFIX + vo.getProjectToken());
    ProjectRedisStorageVo projectRedisStorageVo=JSON.parseObject(redisProject,ProjectRedisStorageVo.class);
   // ProjectRedisStorageVo projectRedisStorageVo = JSON.parseObject(redisProject, ProjectRedisStorageVo.class);
    BeanUtils.copyProperties(vo,projectRedisStorageVo);
    //转化json字符串
    String s = JSON.toJSONString(projectRedisStorageVo);
    redisTemplate.opsForValue().set(ProjectContants.TEMP_PROJECT_PREFIX+vo.getProjectToken(),s);
    return AppResponse.ok("ok");
   }
   //第三步
    @ApiOperation("项目初始化第三步，保存回报")
    @PostMapping("/savereturn")
    public AppResponse saveReturn(@RequestBody List<ProjectReturnVo> pro){
    //1那项目令牌
        String projectToken = pro.get(0).getProjectToken();
       //2.从redis取出临时对象
        String reidsProject = redisTemplate.opsForValue().get(ProjectContants.TEMP_PROJECT_PREFIX + projectToken);
        //3.redisProject-->对象
        ProjectRedisStorageVo projectRedisStorageVo = JSON.parseObject(reidsProject, ProjectRedisStorageVo.class);
      //4,前端的回报保存到集合李
        List list = new ArrayList<>();
        for(ProjectReturnVo projectReturnVo:pro){
            TReturn tReturn=new TReturn();
            BeanUtils.copyProperties(projectReturnVo,tReturn);
            list.add(tReturn);
        }
        //5把集合存入临时对象
        projectRedisStorageVo.setProjectReturns(list);
        String s = JSON.toJSONString( projectRedisStorageVo);
        //6.存入redis
        redisTemplate.opsForValue().set(ProjectContants.TEMP_PROJECT_PREFIX+projectToken,s);
    return AppResponse.ok("成功");
    }
 //保存项目到数据库
    @ApiOperation("保存项目第四步")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accessToken",value = "用户令牌",required = true),
            @ApiImplicitParam(name = "projectToken",value="项目标识",required = true),
            @ApiImplicitParam(name="ops",value="用户操作类型 0-保存草稿 1-提交审核",required = true)})
    @PostMapping("/submit")
    public AppResponse saveProjectInfo(String accessToken,String projectToken,String ops){
        String memberId = redisTemplate.opsForValue().get(accessToken);
        if (memberId.isEmpty()){
            return AppResponse.fail("请登录");
        }
//获取项目
        String redisProject=redisTemplate.opsForValue().get(ProjectContants.TEMP_PROJECT_PREFIX+projectToken);
        //判空处理
        if(StringUtils.isEmpty(redisProject)){
            return AppResponse.fail("没有此项目");
        }
        ProjectRedisStorageVo projectRedisStorageVo = JSON.parseObject(redisProject, ProjectRedisStorageVo.class);


        //保存项目
        if (ops.equals("1")){
            projectCreateService.saveProjectInfo(ProjectStatusEnume.SUBMIT_AUTH,projectRedisStorageVo);
            return AppResponse.ok("提交审核成功");
        }else if(ops.equals("0")){//保存草稿--0
            projectCreateService.saveProjectInfo(ProjectStatusEnume.DRAFT,projectRedisStorageVo);
            return AppResponse.ok("已经存入草稿");
        }else{
            return AppResponse.fail("不允许的操作");
        }
    }
}
